let provice = require('./../../provice.json');
let city = require('./../../city.json');
let parmas = require('./../../data.json');
const { HttpRequest, globalData, CloudFunction, UserAccess, mfutil } = getApp();
Component({
  mixins: [],
  data: {
    memberinfoData: {
      province: '',
      provinceId: '',
      city: '',
      cityId: '',
      interests: '',
      shoeSize: '',
      brandTenure: '',
      email: ''
    },
    pickerActive: false,
    province: [],
    city: [],
    showCity: [],
    userProvince: '',
    userCity: '',
    value: [0, 0, 0],
    router: '/pages/more_info',
    pagetitle: '注册更多信息页',
  },
  props: {},
  async didMount() {
    // let template = await CloudFunction('get', {});

    this.setData({
      template: parmas.data,
      question1List: parmas.data[0].data,
      ageList: parmas.data[3].ageList,
      sizeList: parmas.data[4].sizeList,
      province: provice.province,
      city: city.city,
      showCity: city.city[0],
    })


  },
  didUpdate() {
  },
  didUnmount() {
  },
  methods: {
    questionChange(options) {
      if (options.name == 'memberinfoData.interests') {
        let { question1List } = this.data;
        let interestsZN = {}
        for (let value of question1List) {
          for (let i in options.value) {
            if (i == value.sync_clm_value) {
              interestsZN[value.desc] = '1'
            }
          }
        }
        this.setData({
          interestsZN
        })
      }
      this.setData({
        [options.name]: options.value
      })
    },
    queryMember() {
      this.props.onQueryMember()
    },
    /**
* 省市区遮罩点击
*/
    cityChange() {
      my.hideKeyboard();
      let { pickerActive, value } = this.data;
      this.data.userProvince=this.data.province[value[0]],
      this.data.userCity=this.data.city[value[0]][value[1]]
      this.setData({
        pickerActive: !pickerActive,   //弹窗改变
        // userProvince: this.data.province[value[0]],
        // userCity: this.data.city[value[0]][value[1]],
        value
      })
    },
    /**
 * 省份省市选择
 *  */
    onChange(e) {
      let value = !this.data.city[e.detail.value[0]][e.detail.value[1]] ? [e.detail.value[0], 0, 0] : e.detail.value;
      this.data.showCity = this.data.city[value[0]],
      this.data.userProvince=this.data.province[value[0]],
      this.data.userCity=this.data.city[value[0]][value[1]]
      this.setData({
        value,
        // showCity: this.data.city[value[0]],
        // userProvince: this.data.province[value[0]],
        // userCity: this.data.city[value[0]][value[1]],
      });

    },
    /**
     * 提交省份
    */
    cityChangeSuc() {
      let { pickerActive, userProvince, userCity, userArea, value } = this.data;

      this.setData({
        pickerActive: !pickerActive,   //弹窗改变
        ['memberinfoData.province']: userProvince.name,
        ['memberinfoData.provinceId']: userProvince.code,
        ['memberinfoData.city']: userCity.name,
        ['memberinfoData.cityId']: userCity.code,

        userProvince: userProvince.name,
        userCity: userCity.name,
        userProvinceCode: userProvince.code,
        userCityCode: userCity.code,
        value
      })

    },
    exitChangeSuc() {
      let { pickerActive } = this.data;
      this.setData({
        pickerActive: !pickerActive,   //弹窗改变    
      })
    },
    inputRecord(event) {
      let { value } = event.detail;
      let { item } = event.currentTarget.dataset;
      value = value.replace(/[^0-9A-Za-z@-_\.\/-]/g, '');
      this.setData({
        [item]: value,
      })
    },
    /**
 * input框失去焦点
 * @param {*} event 
 */
    inputBlur(event) {
      let { value } = event.detail;
      let { item } = event.currentTarget.dataset;
      this.setData({
        [item]: value
      })
    },
    focus() {
      this.setData({
        pickerActive: false
      })
    },
    /**
 * 问卷跳转
 * @a a取消
 */
    openQuest(event, a) {
      let item = null;
      if (event) {
        item = event.currentTarget.dataset.item;
      }
      this.setData({
        currentItem: item,
      })
    },
    sucQuestion(question1List) {
      this.setData({
        question1List: question1List
      })
    },
    /**
     * 保存
     */
    async saveFn() {


      let { memberinfoData } = this.data;
      console.log(memberinfoData);
      //邮箱校验
      let reg = /^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*\.[a-z]{2,}$/;
      if (memberinfoData.email && !reg.test(memberinfoData.email) && memberinfoData.email != '') {
        my.showToast({
          content: '请填写正确格式邮箱',
          duration: 1000
        });
        // value = '';
        return
      }
      let path = `/vans-family/api/v2.1/project/vansFamily/clm/forward?url=/v1/member/api/tmall/update&type=PUT`;
      let bodyObj = {
        "memberId": globalData.userInfo.memberId,
        "fullName": globalData.userInfo.fullName,
        "genderCode": globalData.userInfo.genderCode,
        "birthDate": globalData.userInfo.birthDate,
        "email": memberinfoData.email,
        "shoeSize": globalData.userInfo.shoeSize,
        "brandTenure": globalData.userInfo.brandTenure,   //品牌生命周期
        "provinceId": memberinfoData.provinceId,
        "province": memberinfoData.province,
        "cityId": memberinfoData.cityId,
        "city": memberinfoData.city,
        "city": memberinfoData.city,
        "interests": globalData.userInfo.interests
      }
      console.log(bodyObj, 'bodyObjbodyObjbodyObjbodyObj')
      let res = await HttpRequest(path, 'POST', '', bodyObj);
      if (res.data.resultCode == 0) {
        my.showToast({
          type: 'success',
          content: '保存成功',
          duration: 1000,
          success: () => {
            this.setData({
              pickerActive: false
            })
            this.props.onSkipFn()
          },
          fail: () => {
          }
        });
      } else {
        my.alert({
          content: res.data.resultDesc
        })
        // mfutil.launchErrPage(53001)
      }
      /**
           * 用户行为记录埋点
           */
      let routerArr = getCurrentPages();
      let options = {
        grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
        name: routerArr[routerArr.length - 1].data.pagetitle,
      }
      let track = UserAccess(options)
    },
    /**
     * 跳过
     */
    skipFn() {
      this.props.onSkipFn()
    }
  },
});
